package com.baizhi.controller;

import com.baizhi.entity.User;
import com.baizhi.service.UserService;
import com.baizhi.util.JwtUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService us;
    @GetMapping("/login")
    public Map<String,String> login(String name, String pwd){
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("username",name).eq("userpassword",pwd);
        User user = us.getOne(wrapper);
        Map<String,String> map=new HashMap<>();
        if(user==null){
            map.put("msg","登录失败");
        }else{
            map.put("msg","登录成功");
            Map<String,String> jwtMap=new HashMap<>();
            jwtMap.put("userid",user.getId()+"");
            jwtMap.put("name",name);
            jwtMap.put("role",user.getUserisadmin()+"");
            String token = JwtUtils.getToken(jwtMap);
            map.put("token",token);
        }
        return map;
    }

    @GetMapping("/selectOne/{id}")
    public User selectOne(@PathVariable("id") int id){
        return us.getById(id);
    }
}
